<?php 

Class proModel extends AppModel
{
	
	
	public function ordering($val,$id)
	{
		$result=array('error'=>0,'content'=>'');
		$sql="UPDATE ".$this->table('product')." SET ordering='$val' WHERE id='".$id."'";
		$res=$this->query($sql);
		if(!res)
		{
			$result['error']=1;
			$result['content']='';
		}
		return $result;
	}
	public function del_pro($id)
	{
		$result=array('error'=>0,'content'=>'');
		if($id)
		{
			
				$sql="DELETE FROM ".$this->table('product')." WHERE  id='".$id."'";
	
				$res=$this->query($sql);
				 
				$sql="DELETE FROM ".$this->table('product_img')." WHERE  product_id='".$id."'";
				
				$res2=$this->query($sql);
				if(!$res||!$res2)
				{
					$result['error']=1;
					$result['content']='删除失败';
				}
	
	
			
			 
	
	
		}
		return $result;
	}
	public function get_pro_img($id)
	{
		$sql="SELECT original_pic,id FROM ".$this->table('product_img')." WHERE product_id = '".$id."'";
			
		$result=$this->getAll($sql);
		if($result)
		{
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	public function get_pro($id)
	{
		$sql="SELECT * FROM ".$this->table('product')." WHERE id = '".$id."'";
			
		$result=$this->getRow($sql);
		if($result)
		{
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	public function update_pro($info,$img_array,$id)
	{
		$result=array('error'=>0,'content'=>'');
		if($id)
		{
		
			foreach($info as $k=>$v)
			{
				
					$where.=" $k='$v',";
						
				
			}
			
			$where.="modified='".$_SERVER['REQUEST_TIME']."'";
		
			$sql="UPDATE ".$this->table('product')." SET ".$where." WHERE id='".$id."'";
			
		    $res=$this->query($sql);
		    if(!$res)
		    {
		    	$result['error']=1;
		    	$result['content']=Lang('SQL_ERROR');
		    }
		    
		    
		    $sql="SELECT id FROM ".$this->table('product_img')." WHERE product_id='".$id."'";
		    $old_pic=$this->getCol($sql);
		  
		   
		   
		    //删除图片;
		    foreach($old_pic as $k1=>$v1)
		    {
		    	if(!in_array($v1,array_flip($img_array)))
		    	{
		    		
		    		$sql="DELETE FROM ".$this->table('product_img')." WHERE  id='".$v1."'";
		    		$res=$this->query($sql);
		    		
		    		if(!$res)
		    		{
		    			$result['error']=2;
		    			$result['content']=Lang('SQL_ERROR');
		    		}
		    		
		    	}
		    }
		    
		    foreach($img_array as $kk=>$vv)
		    {
		    		if($kk>0)//更新
		    		{
		    			
		    			if(in_array($kk,$old_pic))

		    				$sql="SELECT original_pic FROM ".$this->table('product_img')." WHERE id='".$kk."'";
		    				$old_pic_url=$this->getOne($sql);
		    				if($vv!=$old_pic_url)
		    				{
		    					if(is_file(ROOT_PATH."/".$vv))
		    					{
		    						$middle_pic=$this->make_thumb('/'.$vv,'200','200');
		    						$small_pic=$this->make_thumb('/'.$vv,'80','80');
		    					}
		    			        $sql="UPDATE ".$this->table('product_img')." SET original_pic='".$vv."',middle_pic='".$middle_pic."',small_pic='".$small_pic."'  WHERE id='".$kk."'";
		    			
		    			       $res=$this->query($sql);
		    				}
		    		  
		    		}
		    		else //新增
		    		{
		    			if(is_file(ROOT_PATH."/".$vv))
		    			{
		    				$middle_pic=$this->make_thumb('/'.$vv,'200','200');
		    				$small_pic=$this->make_thumb('/'.$vv,'80','80');
		    			}
		    			$sql="INSERT INTO ".$this->table('product_img')." (product_id,original_pic,middle_pic,small_pic) VALUES('$id','$vv','$middle_pic','$small_pic')";
		    			$res=$this->query($sql);
		    			
		    			if(!$res)
		    			{
		    				$result['error']=3;
		    				$result['content']=Lang('SQL_ERROR');
		    			}
		    		}
				
		    }
		    
		    
		}
		return $result;
	}
	public function create_pro($info,$img_array)
	{
		$field='';
		$value='';
		$result=array('error'=>0,'content'=>'');
		
		
		
			
			foreach($info as $k=>$v)
			{
				if($v!='')
				{
					$field.="$k,";
						
						
					$value.="'$v',";
			
				}
			}
			
			
			$field=substr($field,0,strlen($field)-1);
			$value=substr($value,0,strlen($value)-1);
			
			$sql="INSERT INTO ".$this->table('product')." (".$field.") VALUES(".$value.")";
			if(!$res=$this->query($sql))
			{
				$result['error']=1;
				$result['content']=Lang('SQL_ERROR');
			}
			else 
			{//添加产品图片
				$product_id=$this->insert_id();
				if(count($img_array)>0)
				{
					foreach($img_array as $key=>$val)
					{
						
						
						if(is_file(ROOT_PATH."/".$val))
						{
							$middle_pic=$this->make_thumb('/'.$val,'200','200');
							$small_pic=$this->make_thumb('/'.$val,'80','80');
						}
						$sql="INSERT INTO ".$this->table('product_img')." (product_id,original_pic,middle_pic,small_pic) VALUES('$product_id','$val','$middle_pic','$small_pic')";
						if(!$res=$this->query($sql))
						{
							$result['error']=1;
							$result['content']=Lang('SQL_ERROR');
						}
					}
						
				}
			}
		
	
		return $result;
		//$this->make_thumb('/Uploads/ad/1398673595657549402.jpg','200','200');
	}
	public function pro_list($page=1,$cat_id)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		
		$result=$this->show_page("SELECT id,title,ordering FROM ".$this->table('product').' WHERE cat_id='.$cat_id,$page);
		$listPage['pagelink']=$result['page'];
		$listPage['data']=$result['result'];
		
		
		return $listPage;
	}

	
	
	
}